using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace WisdriContactsAndroid
{
    [Activity(Label = "联系人列表")]
    public class ContactList : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Create your application here
            SetContentView(Resource.Layout.ContactList);
            Bundle b = this.Intent.Extras;

            List<PERSON> personList = new List<PERSON>();
            if (b.ContainsKey("department"))
            {
                personList = SQLiteHelper.GetPersonData().Where(p => p.DEPARTMENT == b.GetString("department")).ToList();
            }
            else if (b.ContainsKey("filter"))
            {
                //                personList = SQLiteHelper.GetPersonData().Where(p => p.PY.ToLower().Contains(b.GetString("filter").ToLower())).ToList();

                personList.Clear();

                var filters = b.GetString("filter").Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                var preference = GetSharedPreferences("settings", FileCreationMode.WorldWriteable);
                var isAdmin = preference.GetBoolean("IsAdmin", false);

                var tempItems = SQLiteHelper.GetPersonData();
                foreach (var filter in filters)
                {
                    tempItems = tempItems.Where(p =>
                        p.ID.Contains(filter) ||
                    p.DEPARTMENT.Contains(filter) ||
                    p.MOBILE_PHONE.Contains(filter) ||
                    p.NAME.Contains(filter) ||
                    p.OFFICE_PHONE.Contains(filter) ||
                    (isAdmin && p.CAR.ToLower().Contains(filter.ToLower())) ||
                    (filter.All(x => char.IsLetter(x)) && p.PY.ToLower().Contains(filter.ToLower()))
                    ).OrderBy(p => p.NAME).ToList();
                }
                tempItems.ForEach(p => personList.Add(p));

            }

            var listViewContact = FindViewById<ListView>(Resource.Id.listViewContact);
            //            listViewContact.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, personList.Select(p => p.NAME).ToArray());

            listViewContact.Adapter = new ContactListViewAdapter(this, personList);

            listViewContact.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) =>
            {
                Activity act = this as Activity;
                Intent intent = new Intent();
                intent.SetClass(act, typeof(Detail));

                Bundle bDetail = new Bundle();
                bDetail.PutString("id", personList[e.Position].ID);
                intent.PutExtras(bDetail);

                act.StartActivity(intent);
            };
        }

        private class ContactListViewAdapter : BaseAdapter<PERSON>
        {
            List<PERSON> items;
            Activity context;

            public ContactListViewAdapter(Activity context, List<PERSON> items)
                : base()
            {
                this.context = context;
                this.items = items;
            }

            public override long GetItemId(int position)
            {
                return position;
            }

            public override PERSON this[int position]
            {
                get { return items[position]; }
            }

            public override int Count
            {
                get { return items.Count; }
            }

            public override View GetView(int position, View convertView, ViewGroup parent)
            {
                var item = items[position];
                View view = convertView;
                if (view == null) // no view to re-use, create new
                    view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem2, null);
                view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.NAME;
                view.FindViewById<TextView>(Android.Resource.Id.Text2).Text = item.ID;

                return view;
            }
        }
    }
}

